package com.itheima.project.face.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.enums.StoreEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.StoreFace;
import com.itheima.project.pojo.Store;
import com.itheima.project.service.IStoreService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StoreFaceImpl.java
 * @Description 门店dubbbo服务实现
 */
@Slf4j
@Service
public class StoreFaceImpl implements StoreFace {

    @Autowired
    IStoreService storeService;

    @Override
    public Page<StoreVo> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) throws ProjectException {
        try {
            // 使用service 查询门店列表
            Page<Store> page = storeService.findStoreVoPage(storeVo, pageNum, pageSize);
            // 查询到的pojo 转 vo分页数据
            return BeanConv.toPage(page, StoreVo.class);
        } catch (Exception e) {
            log.error("查询门店列表异常: {}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.PAGE_FAIL);
        }
    }

    @Override
    public StoreVo createStore(StoreVo storeVo) throws ProjectException {
        try {
            return BeanConv.toBean( storeService.createStore(storeVo), StoreVo.class);
        } catch (Exception e) {
            log.error("保存门店异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.CREATE_FAIL);
        }
    }
    @Override
    public Boolean updateStore(StoreVo storeVo) throws ProjectException {
        try {
            return storeService.updateStore(storeVo);
        } catch (Exception e) {
            log.error("保存门店异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean deleteStore(List<Long> checkedIds) throws ProjectException {
        try {
            return storeService.deleteStore(checkedIds);
        } catch (Exception e) {
            log.error("删除门店异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.DELETE_FAIL);
        }
    }

    @Override
    public StoreVo findStoreByStoreId(Long storeId) throws ProjectException {
        try {
            Store store = storeService.getById(storeId);
            if (!EmptyUtil.isNullOrEmpty(store)){
                return BeanConv.toBean(store,StoreVo.class);
            }
            return null;
        } catch (Exception e) {
            log.error("根据id查询门店信息异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.SELECT_STORE_FAIL);
        }
    }

    @Override
    public List<StoreVo> findStoreVoList() throws ProjectException {
        try {
            return BeanConv.toBeanList(storeService.findStoreVoList(),StoreVo.class);
        } catch (Exception e) {
            log.error("查找门店所有门店异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StoreEnum.SELECT_STORE_LIST_FAIL);
        }
    }
}
